package com.scalar.chiptrack.wipProcessView.dao;

import com.scalar.chiptrack.wipProcessView.ProcessViewGraph;
import com.scalar.chiptrack.wipProcessView.Node;
import com.scalar.chiptrack.wipProcessView.LotInfo;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.utils.SqlQueryMappings;

import com.scalar.ScalarException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * User: Sujan
 * Date: May 12, 2005
 * Time: 12:25:06 PM
 */
public class WipProcessViewDAO
{
	public static ProcessViewGraph getProcessViewGraph ( DBTransaction dbTransaction, String productId ) throws ScalarException
	{
		Connection conn 		= null;
		PreparedStatement pstmt = null;
		ResultSet rset 			= null;
		boolean success			= false;
		ProcessViewGraph processViewGraph = new ProcessViewGraph();
		LinkedList queue 		= new LinkedList();
		try
		{
			if ( dbTransaction == null )
			{
				conn = DataSource.getConnection ( );
			}
			else
			{
				conn = dbTransaction.getConnection ( );
			}
			pstmt = conn.prepareStatement ( SqlQueryMappings.get ( "wipProcessView.getProcessStepId" ) );
			pstmt.setString ( 1, productId );
			rset = pstmt.executeQuery();
			String startProcessStepId = "";
			if ( rset.next( ) )
			{
				startProcessStepId = rset.getString ( "ProcessStep_ID" );
			}
			queue.addLast ( startProcessStepId );
			Node rootNode 	= null;
			com.scalar.chiptrack.wipProcessView.Node childNode 	= null;
			while ( queue.size() != 0 )
			{
				String processStepId = ( String )queue.removeFirst ();
                rootNode = processViewGraph.createNode ( Integer.parseInt ( processStepId ) );
				if ( rset != null )
				{
					try { rset.close(); } catch ( SQLException ignore ) { }
				}
				if ( pstmt != null )
				{
					try { pstmt.close(); } catch ( SQLException ignore ) { }
				}
				pstmt = conn.prepareStatement ( SqlQueryMappings.get ( "wipProcessView.getBackwardFlowProcessStepId" ) );
				pstmt.setString ( 1, processStepId );
				rset = pstmt.executeQuery ();
				while ( rset.next() )
				{
					processStepId = rset.getString ( "ProcessStep_ID" );
					childNode = processViewGraph.createNode ( Integer.parseInt ( processStepId ) );
					processViewGraph.createEdge ( rootNode, childNode );
					queue.addLast ( processStepId );
				}
			}
			success = true;
		}
		catch ( Exception e )
		{
			System.out.println ( "---------Exception while Generating Process View Graph---------" );
			e.printStackTrace();
			throw new ScalarException ( "Could not get processViewGraph", e );
		}
		finally
		{
			if ( dbTransaction == null )
			{
				if ( conn != null )
				{
					if ( success )
					{
						try
						{
							conn.commit();
						}
						catch ( SQLException e )
						{
							throw new ScalarException ( "Could Not Commit", e );
						}
					}
					else
					{
						try
						{
							conn.rollback();
						}
						catch ( SQLException e )
						{
							throw new ScalarException ( "Could Not Rollback", e );
						}
					}
				}
			}
			if ( rset != null )
			{
				try { rset.close(); } catch ( SQLException ignore ) { }
			}
			if ( pstmt != null )
			{
				try { pstmt.close(); } catch ( SQLException ignore ) { }
			}
			if ( conn != null )
			{
				try { conn.close(); } catch ( SQLException ignore ) { }
			}
		}
		return processViewGraph;
	}
	public static void setWipProcessViewNodeInfo ( DBTransaction dbTransaction, com.scalar.chiptrack.wipProcessView.NodeList nodeList, String productId ) throws ScalarException
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		boolean success = false;
		Node node = null;
		LotInfo lot	= null;
		try
		{
			if ( dbTransaction == null )
			{
				conn = DataSource.getConnection ( );
			}
			else
			{
				conn = dbTransaction.getConnection ( );
			}
			pstmt = conn.prepareStatement ( SqlQueryMappings.get ( "wipProcessView.getWipProcessFlowInfo" ) );
			pstmt.setString ( 1, productId );
			rset = pstmt.executeQuery ( );
			int processStepId 	= -1;
			int wipTotalQty		= 0;
			int lotQty			= 0;
			String partNumber 	= null;
			String psType 		= null;
			String psTypeName 	= null;
			String lotNumber	= null;
			String location 	= null;
			String lotStatus	= null;
			boolean lotIsHold	= false;
            String fabLotNumber = null;
            String lotCategory  = null;
            while ( rset.next() )
			{
                processStepId 	= rset.getInt ( "ProcessStep_ID" );
				partNumber		= rset.getString ( "Part_Number" );
				psType			= rset.getString ( "PS_Type" );
				psTypeName		= rset.getString ( "PS_TYPE_NAME" );
				wipTotalQty		= rset.getInt ( "WIP_TOTAL_QTY" );
				lotNumber		= rset.getString ( "LOT_NUMBER" );
				location		= rset.getString ( "LOCATION" );
				lotQty			= rset.getInt ( "LOT_QTY" );
				lotStatus		= rset.getString ( "LOT_STATUS" );
                fabLotNumber    = rset.getString ( "FAB_LOT_NUMBER" );
                lotCategory     = rset.getString ( "LOT_CATEGORY" );
                lotIsHold		= ( "HOLD".equalsIgnoreCase(lotStatus) );
				node = nodeList.getNodeWithId ( processStepId );
				node.setPartNumber ( partNumber );
				node.setPsType ( psType );
				node.setPsTypeName ( psTypeName );
				node.setWipTotalQty ( wipTotalQty );
				if ( lotNumber != null && lotNumber.trim().length() != 0 )
				{
					lot = new LotInfo ( );
					lot.setLotNumber ( lotNumber );
					lot.setLocationName ( location );
					lot.setQuantityIn ( Integer.toString ( lotQty ) );
					lot.setStatus ( lotStatus );
					lot.setHold ( lotIsHold );
                    lot.setFabLotNumber( fabLotNumber );
                    lot.setLotCategory( lotCategory );
                    node.addLot ( lot );
				}
			}
			success = true;
		}
		catch ( Exception e )
		{
			System.out.println ( "----------Exception While Setting the Process view Node Information---------" );
			e.printStackTrace();
			throw new ScalarException ( "Could not set the wip process view node info", e );
		}
		finally
		{
			if ( dbTransaction == null )
			{
				if ( conn != null )
				{
					if ( success )
					{
						try
						{
							conn.commit();
						}
						catch ( SQLException e )
						{
							throw new ScalarException ( "Could not commit", e );
						}
					}
					else
					{
						try
						{
							conn.rollback();
						}
						catch ( SQLException e )
						{
							throw new ScalarException ( "Could not rollback", e );
						}
					}
				}
			}
			if ( rset != null )
			{
				try { rset.close(); } catch ( SQLException ignore ) {}
			}
			if ( pstmt != null )
			{
				try { pstmt.close(); } catch ( SQLException ignore ) { }
			}
			if ( conn != null )
			{
				try { conn.close(); } catch ( SQLException ignore ) { }
			}
		}
	}
}
